﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataAccessor.Tools
{
    /// <summary>
    /// ConfigCollection变化事件
    /// </summary>
    public delegate void ConfigurationCollectionChangedEventHandler();
    public partial class FormConfiguration : Form
    {
        private readonly HolisticDatabase _database;

        #region events
        public event ConfigurationCollectionChangedEventHandler ConfigurationCollectionChanged;
        private void OnConfigurationCollectionChanged()
        {
            if (ConfigurationCollectionChanged != null)
            {
                ConfigurationCollectionChanged();
            }
        }
        public event ConfigurationSettingChangedEventHandler ConfigurationSettingChanged;
        private void OnConfigurationSettingChanged(UCConfiguration sender)
        {
            if (ConfigurationSettingChanged != null)
            {
                ConfigurationSettingChanged(sender);
            }
        }
        #endregion


        public FormConfiguration(HolisticDatabase database)
        {
            InitializeComponent();
            this._database = database;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadSettingForm();
        }

        private void LoadSettingForm()
        {
            List<Control> range = new List<Control>();
            foreach (var config in _database.Config)
            {
                var splitter = BuildSplitter();
                var settingForm = BuildConfigurationControl(config);
                settingForm.Tag = splitter;
                settingForm.MiniMode = true;

                range.Add(splitter);
                range.Add(settingForm);
            }
            panelSettingsContainer.Controls.AddRange(range.ToArray());
        }

        private void pictureCreate_Click(object sender, EventArgs e)
        {
            var splitter = BuildSplitter();
            var settingForm = BuildConfigurationControl();
            settingForm.Tag = splitter;
            List<Control> range = new List<Control> { splitter, settingForm };
            panelSettingsContainer.Controls.AddRange(range.ToArray());

            var config = settingForm.Config;
            var configList = _database.Config.ToList();
            configList.Add(config);
            _database.Config = configList.ToArray();
            OnConfigurationCollectionChanged();
        }

        private void cmiDel_Click(object sender, EventArgs e)
        {
            var settingForm = contextMenu.SourceControl.Parent as UCConfiguration;
            var splitter = settingForm.Tag as Panel;

            var configList = _database.Config.ToList();
            configList.Remove(settingForm.Config);

            _database.Config = configList.ToArray();
            OnConfigurationCollectionChanged();

            settingForm.Parent.Controls.Remove(splitter);
            settingForm.Parent.Controls.Remove(settingForm);

            splitter.Dispose();
            settingForm.Dispose();
        }

        private UCConfiguration BuildConfigurationControl(HolisticDatabaseConfig config = null)
        {
            UCConfiguration settingForm = new UCConfiguration()
            {
                Dock = DockStyle.Top,
                Database = _database,
                Config = config
            };
            // settingForm.Config = config;
            settingForm.Show();
            settingForm.ContextMenuStrip = contextMenu;
            settingForm.ConfigurationSettingChanged += new ConfigurationSettingChangedEventHandler(settingForm_ConfigurationSettingChanged);
            return settingForm;
        }

        void settingForm_ConfigurationSettingChanged(UCConfiguration sender)
        {
            OnConfigurationSettingChanged(sender);
        }

        private Control BuildSplitter()
        {
            Panel splitter = new Panel()
            {
                Dock = DockStyle.Top,
                Height = 5
            };
            return splitter;
        }
    }
}
